//
//  ActivityTableViewCell.m
//  豆瓣
//
//  Created by dllo on 15/12/8.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ActivityTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "DataBaseSingleton.h"


@implementation ActivityTableViewCell

- (void)dealloc{
    [_nameLabel release];
    [_joinLabel release];
    [_addLabel release];
    [_likeLabel release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews{
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 15, 355, 160)];
    [imageView setImage:[UIImage imageNamed:@"bg_eventlistcell.png"]];
    [self.contentView addSubview:imageView];
    
    self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(18, 20, 330, 17)];
    // self.nameLable.backgroundColor = [UIColor purpleColor];
    [self.contentView addSubview:self.nameLabel];
    
    UIImageView *imageView1 =[[UIImageView alloc]initWithFrame:CGRectMake(25, 40, 330, 130)];
    [imageView1 setImage:[UIImage imageNamed:@"bg_share_large.png"]];
    [self.contentView addSubview:imageView1];
    
    
    
    
    
    UIImageView *imageView2 =[[UIImageView alloc]initWithFrame:CGRectMake(30, 43, 20, 20)];
    [imageView2 setImage:[UIImage imageNamed:@"icon_date.png"]];
    [self.contentView addSubview:imageView2];
    self.timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(55, 43, 200, 20)];
    // self.timeLable.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:self.timeLabel];
    
    UIImageView *imageView3 =[[UIImageView alloc]initWithFrame:CGRectMake(30, 65, 20, 20)];
    [imageView3 setImage:[UIImage imageNamed:@"icon_spot.png"]];
    [self.contentView addSubview:imageView3];
    
    self.addLabel = [[UILabel alloc]initWithFrame:CGRectMake(55, 65, 200, 20)];
    //self.addLable.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:self.addLabel];
    
    
    
    UIImageView *imageView4 =[[UIImageView alloc]initWithFrame:CGRectMake(30, 87, 20, 20)];
    [imageView4 setImage:[UIImage imageNamed:@"icon_catalog.png"]];
    [self.contentView addSubview:imageView4];
    
    self.styleLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 87, 200, 20)];
    //self.styleLable.backgroundColor = [UIColor redColor];
//    self.styleLabel.text = @"";
    [self.contentView addSubview:self.styleLabel];
    UILabel *lable1 =[[UILabel alloc]initWithFrame:CGRectMake(55, 87, 200, 20)];
    lable1.text = @"类型：";
    [self.contentView addSubview:lable1];
    
    
    
    
    self.likeLabel = [[UILabel alloc]initWithFrame:CGRectMake(120, 140, 60, 20)];
    
    self.likeLabel.textColor = [UIColor redColor];
//    self.likeLabel.text = @"";
    [self.contentView addSubview:self.likeLabel];
    
    UILabel *lable2 =[[UILabel alloc]initWithFrame:CGRectMake(55, 140, 80, 20)];
    lable2.text = @"感兴趣：";
    [self.contentView addSubview:lable2];
    
    
    
    self.joinLabel = [[UILabel alloc]initWithFrame:CGRectMake(220, 140, 97, 20)];
//    self.joinLabel.text = @"";
    self.joinLabel.textColor = [UIColor redColor];
    [self.contentView addSubview:self.joinLabel];
    
    UILabel *lable3 =[[UILabel alloc]initWithFrame:CGRectMake(175, 140, 80, 20)];
    lable3.text = @"参加：";
    [self.contentView addSubview:lable3];
    
    
    
    
    UIImageView *imageView5 =[[UIImageView alloc]initWithFrame:CGRectMake(270, 43, 75, 110)];
    //[imageView5 setImage:[UIImage imageNamed:@"22.png"]];
    [self.contentView addSubview:imageView5];
    

    self.headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(270, 47, 75, 110)];
    self.headImageView.backgroundColor = [UIColor blackColor];
    [self.contentView addSubview:_headImageView];
    
}


-(void)setActivity:(ActivityList *)activity{
    
    if (_activity!=activity) {
        [_activity release];
        _activity = [activity retain];
    }
    self.nameLabel.text = _activity.title;
    self.timeLabel.text = _activity.begin_time;
    self.addLabel.text = _activity.address;
    self.joinLabel.text = [NSString stringWithFormat:@"%@",_activity.participant_count];
    self.likeLabel.text = [NSString stringWithFormat:@"%@",_activity.wisher_count];
    self.styleLabel.text = _activity.category_name;
//    NSURL *url = [NSURL URLWithString:self.activity.image_hlarge];
//    NSData *data = [NSData dataWithContentsOfURL:url];
//    self.headImageView.image = [UIImage imageWithData:data];
    NSURL *url = [NSURL URLWithString:self.activity.image_hlarge];
    [self.headImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeHoderImage"]];
    
    // 本地缓存
    BOOL flag = NO;
    NSMutableArray *array = [[DataBaseSingleton shareDataBase] selectActivityDetail];
    for (ActivityList *act in array) {
        if ([act.title isEqualToString:self.nameLabel.text]) {
            flag = YES;
            NSLog(@"已经插入");
        }
        
    }
    if (flag == NO) {
        ActivityList *activ = [[ActivityList alloc] init];
        activ = self.activity;
        [[DataBaseSingleton shareDataBase] insertActivityDetailIntoTable:activ];
    }
    
    
}


@end
